package com.ftg.learn.day10.factory.last;

import com.ftg.learn.day09.exam02.Dog;
import com.ftg.learn.day10.factory.homework.BeanInfo;
import com.ftg.learn.day10.factory.homework.Fly;
import com.ftg.learn.day10.factory.homework.Lion;

/**
 * javabean - java类--类工厂
 *
 * :  就是为了返回对应类的实例
 *
 * @author kangjx
 * @version 1.0
 * @date 2023/8/15 上午10:09
 */
public class BeanFactory {

    //应该有一个存储大量类实现的数组
    private Object[] obj;

    //有一个方法接收大量的类的关联对象。然后实现化以后存储到obj里面

    public BeanFactory(Class<?>...classes){

        this.obj = new Object[classes.length];

        for (int i = 0; i < classes.length; i++) {
            Class<?> temp = classes[i];
            try {
                Object obj = temp.newInstance();
                this.obj[i] = obj;
            } catch (InstantiationException | IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public <E> E getInstance(Class<E> c){
        //从obj里面取出来这个实例
        for (int i = 0; i < this.obj.length; i++) {
            if(this.obj[i].getClass() == c){
                return (E)this.obj[i];
            }
        }
        return null;
    }

    public static void main(String[] args) {
        BeanFactory bf  = new BeanFactory(Dog.class, Lion.class);

        Fly instance = bf.getInstance(Fly.class);
        instance.setName("abc");
        System.out.println(instance);


    }

}
